package view.parameterpanels;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.IManufacturingMethod;
import net.miginfocom.swing.MigLayout;
import view.AlphaTextField;
import view.NumericalTextField;
import view.CenterTabbedPane.EBenchmarkTab;

//#Neu
/**
 * To draw a parameter panel for input parameters of type geometry parameters.
 * This panel can be used when multiple sets/part processes have to be added to
 * a geometry set. The user can assign a name to each part process of the
 * geometry set.
 * @author Sebastian Perlwitz
 *
 */
public class GeometryPanelWithSets extends StandardParameterInputPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The text fields that allow the user assigning a geometry set's name.
	 */
	private List<AlphaTextField> descriptionTextFields;
	
	/**
	 * A button to add a geometry set.
	 */
	private JButton plusButton;
	
	/**
	 * A button to remove a geometry set.
	 */
	private JButton subtractButton;
	
	/**
	 * To create a new {@code GeometryPanelWithSets}.
	 * @param logoPath a path to an optional logo. If passed {@code null} a
	 * standard logo is used.
	 * @param tab the type of input parameters that is displayed on the panel.
	 * @param panelTitle the panel's title.
	 * @param manuMethod the model to be referenced.
	 */
	public GeometryPanelWithSets(
			String logoPath,
			EBenchmarkTab tab,
			String panelTitle,
			IManufacturingMethod manuMethod) {
		super(logoPath, tab, panelTitle, manuMethod);
		descriptionTextFields = new ArrayList<AlphaTextField>();
	}
	
	@Override
	protected void drawSettings() {
		settings.add(new JLabel("Anzahl der Zonen festlegen: ")); //Generalize! 
		// At the moment this description only makes sense for ReflowSoldering.
		
		plusButton = new JButton("+");
		subtractButton = new JButton("-");
		
		settings.add(plusButton);
		settings.add(subtractButton);
	}
	
	/**
	 * 
	 * @param paramInfo the parameters' information:<br>
     * paramInfo[0] = parameter's name<br>
     * paramInfo[1] = parameter's key for identification<br>
     * paramInfo[2] = parameter's abbreviation<br>
     * paramInfo[3] = parameter's unit<br>
     * paramInfo[4] = parameter's initial value
     * @param borderTitle the parameters' description.
	 * @param partProcKey the identifying key to the geometry set's part
     * process.
	 * @param textPartProcDescription the name of the part process description
	 * text field. For example the text field "Zonenbeschreibung" in
	 * {@code PartProcessReflowSoldering}.
	 * @return the newly created text fields.
	 */
	public List<JTextField> addParameterSet(
			List<String[]> paramInfo,
			String borderTitle,
			String partProcKey,
			String textPartProcDescription) {
		List<JTextField> newTextFields = new ArrayList<JTextField>();
		JPanel parameterPanel = new JPanel();
    	parameterPanel.setLayout(new MigLayout("wrap 4"));
		
    	// Add process description text field.
    	parameterPanel.add(new JLabel(textPartProcDescription));
    	parameterPanel.add(new JLabel(""));
    	parameterPanel.add(new JLabel(""));
    	
    	AlphaTextField alphaTF = new AlphaTextField(partProcKey);
    	alphaTF.setText(borderTitle);    	
    	alphaTF.setCaretPosition(0);    	
    	parameterPanel.add(alphaTF);
    	descriptionTextFields.add(alphaTF);
    	newTextFields.add(alphaTF);
    	
    	// Add standard parameter panels.
    	newTextFields.addAll(
        		addStandardParameterPanels(
        				parameterPanel, paramInfo, borderTitle));
    	
    	return newTextFields;
    }
	
	@Override
	public void update() {
				
		super.update();		
		
		if(getErrorStatus()) {
			// To disable the buttons.
			plusButton.setEnabled(false);
			subtractButton.setEnabled(false);
		} else {
			// To enable the buttons after the input was corrected.
			if(!plusButton.isEnabled()) {
				plusButton.setEnabled(true);
				subtractButton.setEnabled(true);
			}
		}
		// To deactivate the geometry set name fields.
		if(getErrorStatus()) {
			// To disable all text fields because of wrong input
			for(AlphaTextField txtField : descriptionTextFields) {
				if(!txtField.getErrorStatus()) {
					txtField.setEnabled(false);
				}
			}
		} else {
			// To enable all disabled text fields when wrong input was corrected
			for(AlphaTextField txtField : descriptionTextFields) {
				if(!txtField.isEnabled()) {
					txtField.setEnabled(true);
				}
			}
		}
	}
	
	/**
	 * To get the text fields that describe the geometry set.
	 * @return The text fields.
	 */
	public List<AlphaTextField> getDescriptionTextFields() {
		return descriptionTextFields;
	}
	
	/**
	 * To get the "plus" button of this panel.
	 * @return The button.
	 */
	public JButton getPlusButton() {
		return plusButton;
	}
	
	/**
	 * To get the "subtract" button of this panel.
	 * @return The button.
	 */
	public JButton getSubtractButton() {
		return subtractButton;
	}
	
	@Override
	public List<JTextField> removeParameterPanel() {
		
		int panelsCount = parameterPanels.size() - 1;
		List<JTextField> removedTextFields = 
				new ArrayList<JTextField>();
		List<NumericalTextField> removedNumericalTextFields = 
				new ArrayList<NumericalTextField>();
		List<AlphaTextField> removedAlphaTextFields = 
				new ArrayList<AlphaTextField>();
		
		if(panelsCount > 0) {			
			JPanel panelToRemove = parameterPanels.get(panelsCount);
			paramPanel.remove(panelToRemove);
			for(Component component : panelToRemove.getComponents()) {
				if(component instanceof NumericalTextField) {
					removedNumericalTextFields.add((NumericalTextField) component);
					removedTextFields.add((JTextField) component);
				} else if (component instanceof AlphaTextField) {
					removedAlphaTextFields.add((AlphaTextField) component);
					removedTextFields.add((JTextField) component);
				}
			}
			textFields.removeAll(removedNumericalTextFields);
			descriptionTextFields.removeAll(removedAlphaTextFields);
			parameterPanels.remove(panelsCount);
			this.revalidate();
		}
		return removedTextFields;
	}

}
